package com.hh.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.hh.item.dao.IUserDao;
import com.hh.item.pojo.User;
import com.hh.item.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangfan
 * @date 2020/3/7 13:56
 * @description
 */

@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao userDao;

    /**
     * @param userName
     * @param userPwd
     * @return com.hh.item.pojo.User
     * @description 用户登陆校验
     */
    @Override
    public User login(String userName, String userPwd) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userName", userName);

        List<User> user = userDao.selectByExample(example);

        if (user.isEmpty()) {
            return null;
        } else if (user.size() == 1 && userPwd.equals(user.get(0).getUserPwd())) {
            return user.get(0);
        }

        return null;
    }

    /**
     * @param userName
     * @return java.lang.String
     * @description 注册前检验用户名是否存在
     */
    @Override
    public String check(String userName) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userName", userName);

        List<User> user = userDao.selectByExample(example);

        return user.isEmpty() ? "true" : "false";
    }

    /**
     * @param user
     * @return java.lang.String
     * @description 用户注册
     */
    @Override
    public String regist(User user) {
        return userDao.insertSelective(user) == 1 ? "true" : "false";
    }

    /**
     * @param user
     * @return java.lang.String
     * @description 更新用户信息
     */
    public String update(User user) {
        return userDao.updateByPrimaryKeySelective(user) == 1 ? "true" : "false";
    }

    /**
     * @param userId
     * @param searchText
     * @return java.lang.String
     * @description 按照用户名模糊查找用户信息 并返回与查询者的关系
     */
    @Override
    public String searchUser(String userId, String searchText) {
        List<User> userList = userDao.searchUser("%" + searchText + "%");
        if (userList.isEmpty()) {
            return null;
        } else {
            for (User user : userList) {
                user.setUserRelation(this.getUsersRelation(userId, user.getUserId().toString()));
            }
            return JSON.toJSONString(userList);
        }
    }

    /**
     * @param userId
     * @param targetId
     * @return java.lang.Integer
     * @description 获取两个用户的关系
     */
    public Integer getUsersRelation(String userId, String targetId) {
        int status = -1;
        List<Integer> relationStatus = userDao.getRelationStatus(userId, targetId);
        if (!relationStatus.isEmpty()) {
            if (relationStatus.contains(1)) {
                status = 1;
                if (relationStatus.contains(0)) {
                    status = 2;
                }
            } else if (relationStatus.contains(0)) {
                status = 0;
            }
        }
        return status;
    }

    /**
     * @param userId
     * @param targetId
     * @return java.lang.String
     * @description 关注
     */
    @Override
    public String follow(String userId, String targetId) {

        String result = "false";

        boolean alreadyFollow = userDao.getRelationStatus(userId, targetId).contains(1)
                && userDao.getRelationStatus(targetId, userId).contains(0);
        if (!alreadyFollow) {
            int isAddFollow = userDao.addFollow(userId);
            int isAddFan = userDao.addFan(targetId);
            int isFollow = userDao.follow(userId, targetId);
            int isFan = userDao.fan(targetId, userId);

            if (isAddFollow == 1 && isAddFan == 1 && isFollow == 1 && isFan == 1) {
                result = "true";
            }
        }
        return result;
    }

    /**
     * @param userId
     * @param targetId
     * @return java.lang.String
     * @description 取关
     */
    @Override
    public String unfollow(String userId, String targetId) {

        String result = "false";

        boolean alreadyFollow = userDao.getRelationStatus(userId, targetId).contains(1)
                && userDao.getRelationStatus(targetId, userId).contains(0);
        if (alreadyFollow) {
            int isCancelFollow = userDao.cancelFollow(userId);
            int isCancelFan = userDao.cancelFan(targetId);
            int isUnFollow = userDao.unfollow(userId, targetId);
            int isUnFan = userDao.unfan(targetId, userId);

            if (isCancelFollow == 1 && isCancelFan == 1 && isUnFollow == 1 && isUnFan == 1) {
                result = "true";
            }
        }
        return result;
    }

    @Override
    public User edit(String oldName, String userName, String userPwd, Integer userSex) {
        if (userDao.editMine(oldName,userName,userPwd,userSex) == 1) {
            Example example = new Example(User.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("userName", userName);

            List<User> user = userDao.selectByExample(example);

            if (user.isEmpty()) {
                return null;
            } else if (user.size() == 1 && userPwd.equals(user.get(0).getUserPwd())) {
                return user.get(0);
            }
        }
        return null;
    }

    @Override
    public User getMine(Integer userId) {
        return userDao.getMine(userId);
    }

    @Override
    public List<User> getFollow(Integer userId) {
        return userDao.getFollow(userId);
    }

    @Override
    public List<User> getFans(Integer userId) {
        return userDao.getFans(userId);
    }

    @Override
    public List<User> searchAllNameAndId() {
        return userDao.searchAllNameAndId();
    }


    /*@Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public int save(User user) {
        return userDao.save(user);
    }

    @Override
    public int updateUserById(User user) {
        return userDao.updateUserById(user);
    }

    @Override
    public int deleteUserById(Integer id) {
        return userDao.deleteUserById(id);
    }

    @Override
    public User findUserById(Integer id) {
        return userDao.findUserById(id);
    }

    @Override
    public Map<String, String> refresh(String userId) {
        HashMap<String, String> result = (HashMap<String, String>) userDao.refresh(userId);
        return result.isEmpty() ? null : result;
    }

    @Override
    public Map<String, Object> refreshf(String userId, String targetId) {
        HashMap<String, String> rs = (HashMap<String, String>) userDao.refresh(targetId);
        Map<String, Object> result = new HashMap<>();
        result.put("fan_follow_map", rs);
        result.put("user_relation", String.valueOf(this.getUsersRelation(userId, targetId)));
        return result;
    }

    @Override
    public List<User> testTk() {
        return userDao.selectAll();
    }

    */
}
